package cn.csust.net2.manager.shared.xml;

import java.util.ArrayList;
import java.util.List;
import cn.csust.net2.manager.shared.util.Utils;
import com.google.gwt.xml.client.Attr;
import com.google.gwt.xml.client.Comment;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.NamedNodeMap;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;

public class AssistFactory {
	/**
	 * 标识：注释为有效标识
	 */
	public static final String SYMBOL = "@";
	/**
	 * 标识：根节点的标识
	 */
	public static final String ROOT_TAR = "class";
	/**
	 * 标识：子节点的继承标识
	 */
	public static final String SUBCLASS_TAR = "joined-subclass";
	/**
	 * 标识：标签共有的名称标识
	 */
	public static final String NAME_TAR = "name";
	/**
	 * 规定：在各个属性之前用，号隔开
	 */
	public static final String COMMENT_COMMA = "\\,";
	/**
	 * 规定：在属性名称和值之间用=号连接
	 */
	public static final String COMMENT_SAME = "\\=";
	/**
	 * 标识：class的标签
	 */
	public static final String BASE_PACKAGE="cn.csust.net2.manager.shared.po.";
	/**
	 * 规定：SIMPLEBOX的分割
	 */
	public static final String COMMENT_SIMPLEBOX_ARRAY="\\;";
	/**
	 * 
	 * @param xmlStr
	 * @return
	 */
	public static ClassAssist getClassAssist(XmlInfo xmlInfo){
		String xmlStr = xmlInfo.getTextResource().getText();
		String subclass = xmlInfo.getSubclass();
		ClassAssist classAssist = new ClassAssist();
		List<PropertyAssist> propertyAssists = new ArrayList<PropertyAssist>();
		Document dom = XMLParser.parse(xmlStr);
		NodeList nodes = dom.getElementsByTagName(AssistFactory.ROOT_TAR);
		//只有一个CLASS时候
		if(nodes.getLength()==1){
			String className = nodes.item(0).getAttributes().getNamedItem(AssistFactory.NAME_TAR).getNodeValue();
			if(className!=null&&!className.equals(""))
				classAssist.setClassName(AssistFactory.BASE_PACKAGE+className);
			Node node = nodes.item(0).getFirstChild();
			PropertyAssist propertyAssist = null;
			boolean match = false;
			while(node.getNextSibling()!=null){
				node = node.getNextSibling();
				//如果是子类继承的配置
				if(node.getNodeName().equals(AssistFactory.SUBCLASS_TAR)){

					if(subclass==null||!node.getAttributes().getNamedItem(AssistFactory.NAME_TAR).getNodeValue().equals(subclass))
						
						continue;
					
					else
						node = node.getFirstChild();
					
				}
				//如果是注释
				if(node instanceof Comment&&!match){
					
					propertyAssist = new PropertyAssist();
					Comment c = (Comment)node;
					boolean ok = AssistFactory.prase(c,propertyAssist);
					System.out.println(""+propertyAssist.getDisplayName()+""+propertyAssist.getEditorType());
					if(ok)
						match = true;
				}
				//如果是属性节点
				else if(node instanceof Element&&match){
					/**
					 * Element只记录了显示的属性，父类NODE记录了所有的属性
					 */
					AssistFactory.prase(node,propertyAssist);
					propertyAssists.add(propertyAssist);
					match = false;
				}
			}
//			for(PropertyAssist pp:propertyAssists){
//				System.out.println(pp.getDisplayName()+":::::asdasd"+pp.getEditorType());
//			}
			classAssist.setPropertyAssists(propertyAssists);
			
		}
		return classAssist;
	}
	/**
	 * 
	 * @param c
	 * @param propertyAssist
	 * @return
	 */
	//解析comment的内容
	public static boolean prase(Comment c,PropertyAssist propertyAssist){
		
		String org = Utils.trim( c.getData());
	//	System.out.println("测试的整个单位注释为"+org);
		if(!org.startsWith(AssistFactory.SYMBOL))
			return false;
		org.replaceFirst(AssistFactory.SYMBOL, "");
		String [] modules = Utils.split(org, AssistFactory.COMMENT_COMMA); 
		System.out.println("注释单元的模块数为"+modules.length);
		for(int i=0;i<modules.length;i++){
			 String module = modules[i].trim(); 
//			 System.out.println("sssss:"+module);
			 String [] keyValue = Utils.split(module, AssistFactory.COMMENT_SAME);
			 if(keyValue.length==2){
				 String key = keyValue[0].trim();
				 String value = Utils.Remove(keyValue[1].trim());
//				 String value = keyValue[1].trim();
				 System.out.println("key为"+key+"值为wc"+value);
				 propertyAssist.set(key, value);
			 }
		}		
		return true;
	}
	/**
	 * 
	 * @param element
	 * @param propertyAssist
	 * @return
	 */
	//解析ELEMENT的内容
	public static boolean prase(Node element,PropertyAssist propertyAssist){
		
		NamedNodeMap a = element.getAttributes();
		
		for(int i =0 ;i<a.getLength();i++){
			Attr att = (Attr)a.item(i);
			String key = att.getName();
			String value = att.getValue();
		//	System.out.println(key+":"+value);
			propertyAssist.set(key, value);	
		}
		return true;
	}
}
